package com.liruo.idworker.distributed.twitter;

import com.liruo.idworker.distributed.twitter.strategy.MachineIDStrategy;

/**
 * @Author:liruo
 * @Date:2023-02-06-20:05:26
 * @Desc
 */
public abstract class AbstractMachineIdStrategy implements MachineIDStrategy {
    protected final int dataCenterId;
    protected final int inCenterMachineId;
    protected  String dataCenterName;
    protected  String machineName;
    protected final int machineId;
    public AbstractMachineIdStrategy(int dataCenterId, int inCenterMachineId) {
        this.dataCenterId = dataCenterId;
        this.inCenterMachineId = inCenterMachineId;
        this.machineId = assemblyMachineID();
    }
    @Override
    public long getMachineID() {
        return this.machineId;
    }

    protected abstract int assemblyMachineID();


    public void setDataCenterName(String dataCenterName) {
        this.dataCenterName = dataCenterName;
    }

    public void setMachineName(String machineName) {
        this.machineName = machineName;
    }

    @Override
    public String toString() {
        return "%s { dataCenter[%sid=%d], machine[%sid=%d]}".formatted(
                getClass().getName(),
                dataCenterName == null ? "" : dataCenterName + ":",
                dataCenterId,
                machineName == null ? "" : machineName + ":",
                inCenterMachineId
        );
    }
}
